package com.promise.jokerdream.work.signin

import com.promise.jokerdream.manager.ExecutionHistoryManager
import com.promise.jokerdream.repository.GameApi
import com.promise.jokerdream.repository.foldWithApiException
import com.promise.jokerdream.utils.AppContext
import com.promise.jokerdream.work.BaseWork
import com.promise.jokerdream.work.WorkResult

// 乐斗菜单工作
class ActMenuWork(override val uin: String): BaseWork(uin) {

    override val name: String
        get() = "乐斗菜单"

    override val priority: Int
        get() = 3

    override val isActWork: Boolean
        get() = true

    private val gameApi = GameApi.Companion.getInstance()
    private val historyManager by lazy { ExecutionHistoryManager.Companion.getInstance(AppContext.get()) }

    override suspend fun executeWork(): WorkResult {
        return try {
            // 查询乐斗菜单信息
            val queryResult = gameApi.queryActMenu()

            queryResult.foldWithApiException(
                onSuccess = { response ->
                    // 检查是否需要领取礼物
                    if (response.today == 0) {
                        // 解析已领取的礼物ID列表
                        val claimedGifts = if (response.gift.isNotEmpty()) {
                            response.gift.split(",").mapNotNull { it.trim().toIntOrNull() }
                        } else {
                            emptyList()
                        }

                        // 如果已领取的礼物少于5个，则从未领取的里选择一个领取
                        if (claimedGifts.size < 5) {
                            // 找出未领取的礼物ID（礼物ID是1-5）
                            val allGiftIds = (1..5).toList()
                            val unclaimedGifts = allGiftIds.filter { it !in claimedGifts }

                            if (unclaimedGifts.isNotEmpty()) {
                                // 选择第一个未领取的礼物
                                val giftToClaim = unclaimedGifts.first()
                                
                                // 领取礼物
                                val claimResult = gameApi.claimActMenuGift(giftToClaim)
                                claimResult.foldWithApiException(
                                    onSuccess = { claimResponse ->
                                        historyManager.addHistory(name, claimResponse.getDescription())
                                    },
                                    onFailure = { code, msg ->
                                        historyManager.addHistory(name, msg)
                                    }
                                )
                            }
                        }
                    }
                    WorkResult.Success
                },
                onFailure = { code, msg ->
                    historyManager.addHistory(name, msg)
                    WorkResult.Failure(Exception(msg), msg)
                }
            )
        } catch (e: Exception) {
            historyManager.addHistory(name, e.message)
            WorkResult.Failure(e, e.message)
        }
    }
}